Support matchers and regexps for the source property (#980)#47
Open
tas50 wants to merge 1 commit into
Open
Conversation
`matches_parameter?` special-cases the `source` property as
`Array(expected) == Array(value)` because Chef may store source as an
Array. But wrapping the expectation in an Array and comparing with `==`
means an RSpec matcher or Regexp expectation can never match: e.g.
`.with(source: end_with("MyFile.pem"))` became
`[<EndWith matcher>] == ["C:/MyFile.pem"]`, which is always false.
Try `expected === value` first so matchers, Regexps, and exact values
all work against a scalar source, then keep the array-normalized
comparison as a fallback so a plain string still matches a single-element
array source (the original behavior).
Signed-off-by: Tim Smith <tsmith84@proton.me>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes chefspec#980 —
.with(source: <matcher>)never matches.ResourceMatcher#matches_parameter?special-cases thesourceproperty:The intent is to let a plain string match a source that Chef stored as an Array. But wrapping the expectation in an Array and comparing with
==means an RSpec matcher or Regexp can never match. For the reporter's case:Every other property routes through
expected === value, which is exactly what makes matchers/Regexps work —sourcewas the one exception.Fix
Try
expected === valuefirst (so RSpec matchers, Regexps, and exact scalar/array values all work), then fall back to the array-normalizedArray(expected) == Array(value)so a plain string still matches a single-element array source — preserving the original behavior.Testing
:sourcematching (TDD — the matcher and Regexp cases failed first): exact string, plain-string-vs-array, exact array, an RSpec matcher against a scalar source (the issue), a Regexp, and a non-matching matcher (stays false — no over-matching).bundle exec rspec spec/unit/→167 examples, 0 failures.cookbook_file,remote_file,template,render_file) →116 examples, 0 failures(array-source path unregressed).cookstyle --chefstyle -c .rubocop.yml→ no offenses.Note
Touches
spec/unit/matchers/resource_matcher_spec.rb, which is also rewritten by #31 and #42. Whichever merges first, the others need theirdescribeblocks combined into the one file.